#!/bin/bash
# Author: fanyongfeng
# 安装minio
#单机模式安装sh /root/minio_install.sh --prefix=/data/minio --mode=single
#集群模式安装sh /root/minio_install.sh --prefix=/data/minio --mode=cluster --ip_list=192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4 --password=your_strong_password

set -e
# 使用root用户执行
[[ $(id -u) -ne 0 ]] && echo "请使用root用户执行此脚本" && exit 1

usage(){
    echo "Usage:"
    echo "sh $0
            --prefix=安装目录(默认/data/minio)
            --mode=single|cluster
            --ip_list=集群ip列表,用逗号分割
            --password=MINIO管理员密码(单节点模式时可不提供，自动生成密码),集群模式密码保持一致
        "
    exit 1
}

#处理参数，规范化参数
ARGS=$(getopt -o h --long prefix:,mode:,ip_list:,password:,help -n "$0" -- "$@")
if [[ $? != 0 || $# -eq 0 ]]
then
        usage
fi

#重新排列参数顺序
eval set -- "${ARGS}"
#通过shift和while循环处理参数
while true
do
    case $1 in
        --prefix)
            PREFIX=$2
            shift 2
            ;;
        --mode)
            MODE=$2
            shift 2
            ;;
        --ip_list)
            IP_LIST=$2
            shift 2
            ;;
        --password)
            PASSWORD=$2
            shift 2
            ;;
        -h|-help)
            usage
            exit 1
            ;;
        --)
           shift
           break
           ;;
        *)
           echo "Internal error!"
           exit 1
           ;;
    esac
done

if [[ ${MODE} == cluster ]]
then
    if [[ -z ${IP_LIST} || -z ${PASSWORD} ]]
    then
        echo "集群模式，请提供集群ip列表和集群密码，集群模式至少需要4台服务器"
        usage
    fi
fi

INSTALL_DIR=${PREFIX:-/data/minio}
RUN_USER="minio"
HOST_IP=$(ip a | grep global | head -1 | awk -F'/' '{print $1}' | awk '{print $2}')

# 关闭防火墙和selinux
systemctl stop firewalld && systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce  0

# 下载安装包
[[ -f minio ]] && mv minio minio.bak || wget -4 https://dl.minio.org.cn/server/minio/release/linux-amd64/minio
#[[ -f mc ]] || wget https://dl.minio.org.cn/client/mc/release/linux-amd64/mc

if [[ $? -ne 0 ]]
then
    echo "minio下载失败，请重试或手动上传此安装包"
    exit 1
fi

# 创建相关目录
mkdir -p ${INSTALL_DIR}/{bin,data}
mv minio.bak ${INSTALL_DIR}/bin/minio
#mv mc  ${INSTALL_DIR}/bin
chmod +x ${INSTALL_DIR}/bin/minio
#chmod +x ${INSTALL_DIR}/bin/mc

# 添加运行账户并授权
useradd ${RUN_USER} -s /sbin/nologin
chown -R ${RUN_USER}.${RUN_USER} ${INSTALL_DIR}

# 添加minio环境变量文件
# 生成12个字符长的随机密码
[[ -z ${PASSWORD} ]] && PASSWORD="$(gpg --gen-random --armor 1 12)"

# 设置单节点与集群模式的MINIO_VOLUMES
if [[ -z ${IP_LIST} ]]
then
    MINIO_VOLUMES=${INSTALL_DIR}/data
else
    for i in $(echo "${IP_LIST}" | sed "s#,#:9000/${INSTALL_DIR} #g")
    do
        MINIO_VOLUMES="http://$i $MINIO_VOLUMES "
    done
fi

cat > /etc/minio <<EOF
# 设置Minio文件存储位置，必须存在且为空文件夹
# 单节点
# MINIO_VOLUMES=${INSTALL_DIR}/data
# 集群模式，至少4块盘
MINIO_VOLUMES="http://192.168.10.10:9000/data/minio http://192.168.10.20:9000/data/minio http://192.168.10.30:9000/data/minio http://192.168.10.40:9000/data/minio"
MINIO_OPTS="--console-address ${HOST_IP}:9001"
MINIO_ROOT_USER=admin
MINIO_ROOT_PASSWORD=Minio@2025
EOF

chown ${RUN_USER}.${RUN_USER} /etc/minio


# 使用systemd管理minio
cat > /etc/systemd/system/minio.service << EOF
Description=MinIO
After=network.target

[Service]
User=${RUN_USER}
Group=${RUN_USER}
EnvironmentFile=-/etc/minio
ExecStart=${INSTALL_DIR}/bin/minio server \$MINIO_OPTS \$MINIO_VOLUMES
ExecReload=/bin/kill -HUP \$MAINPID
KillMode=process
Restart=on-failure
RestartSec=30s
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable minio
systemctl start minio
sleep 2

echo "minio 存储API访问地址: ${HOST_IP}:9000"
echo "minio web控制台访问地址: ${HOST_IP}:9001"
echo "minio管理员用户名密码: admin / ${PASSWORD}"

echo "nginx代理minio配置,参考如下链接："
echo "https://min.io/docs/minio/linux/integrations/setup-nginx-proxy-with-minio.html"
systemctl status minio
# 参考文档：
# 1. 官方安装文档：https://www.minio.org.cn/docs/minio/linux/operations/installation.html
# 2. nginx中配置minio：https://min.io/docs/minio/linux/integrations/setup-nginx-proxy-with-minio.html